home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cxref_1_4a.lha / latex.c < prev    next >
C/C++ Source or Header  |  1997-12-07  |  37KB  |  1,276 lines

  1. /***************************************
  2.   $Header: /home/amb/cxref/RCS/latex.c 1.27 1997/11/20 19:19:07 amb Exp $
  3.  
  4.   C Cross Referencing & Documentation tool. Version 1.4a.
  5.  
  6.   Writes the Latex output.
  7.   ******************/ /******************
  8.   Written by Andrew M. Bishop
  9.  
  10.   This file Copyright 1995,96,97 Andrew M. Bishop
  11.   It may be distributed under the GNU Public License, version 2, or
  12.   any higher version.  See section COPYING of the GNU Public license
  13.   for conditions under which this file may be redistributed.
  14.   ***************************************/
  15.  
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <unistd.h>
  22.  
  23. #ifdef AMIGA /* olsen */
  24. #include "amiga.h"
  25. #endif /* AMIGA */
  26.  
  27. #ifndef min
  28. #define min(x,y) ( (x) < (y) ? (x) : (y) )
  29. #endif
  30.  
  31. #include "memory.h"
  32. #include "datatype.h"
  33. #include "cxref.h"
  34.  
  35. /*+ The name of the output tex file that includes each of the others. +*/
  36. #define LATEX_FILE        ".tex"
  37. #define LATEX_FILE_BACKUP ".tex~"
  38.  
  39. /*+ The name of the output tex file that contains the appendix. +*/
  40. #define LATEX_APDX        ".apdx"
  41.  
  42. /*+ The comments are to be inserted verbatim. +*/
  43. extern int option_verbatim_comments;
  44.  
  45. /*+ The type of LaTeX output to produce. +*/
  46. extern int option_latex;
  47.  
  48. /*+ The name of the directory for the output. +*/
  49. extern char* option_odir;
  50.  
  51. /*+ The base name of the file for the output. +*/
  52. extern char* option_name;
  53.  
  54. extern char *latex_fonts_style,*latex_page_style,*latex_cxref_style;
  55.  
  56. static void WriteLatexFilePart(File file);
  57. static void WriteLatexInclude(Include inc);
  58. static void WriteLatexSubInclude(Include inc,int depth);
  59. static void WriteLatexDefine(Define def);
  60. static void WriteLatexTypedef(Typedef type,char* filename);
  61. static void WriteLatexStructUnion(StructUnion su,int depth);
  62. static void WriteLatexVariable(Variable var,char* filename);
  63. static void WriteLatexFunction(Function func,char* filename);
  64.  
  65. static void WriteLatexDocument(char* name,int appendix);
  66. static void WriteLatexTemplate(char* name);
  67.  
  68. static char* latex(char* c);
  69.  
  70. /*+ The output file for the latex. +*/
  71. static FILE* of;
  72.  
  73. /*+ Counts the lines in a table to insert breaks. +*/
  74. static int countlines=0;
  75.  
  76. /*++++++++++++++++++++++++++++++++++++++
  77.   Write a Latex file for a complete File structure and all components.
  78.  
  79.   File file The File structure to output.
  80.   ++++++++++++++++++++++++++++++++++++++*/
  81.  
  82. void WriteLatexFile(File file)
  83. {
  84.  char* ofile;
  85.  
  86.  /* Write the including file. */
  87.  
  88.  WriteLatexDocument(file->name,0);
  89.  
  90.  /* Open the file */
  91.  
  92.  ofile=ConcatStrings(4,option_odir,"/",file->name,LATEX_FILE);
  93.  
  94.  of=fopen(ofile,"w");
  95.  if(!of)
  96.    {
  97.     struct stat stat_buf;
  98.     int i,ofl=strlen(ofile);
  99.  
  100.     for(i=strlen(option_odir)+1;i<ofl;i++)
  101.        if(ofile[i]=='/')
  102.          {
  103.           ofile[i]=0;
  104.           if(stat(ofile,&stat_buf))
  105.              mkdir(ofile,S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
  106.           ofile[i]='/';
  107.          }
  108.  
  109.     of=fopen(ofile,"w");
  110.    }
  111.  
  112.  if(!of)
  113.    {fprintf(stderr,"cxref: Failed to open the LaTeX output file '%s'\n",ofile);exit(1);}
  114.  
  115.  /* Write out a header. */
  116.  
  117.  fputs("% This LaTeX file generated by cxref\n",of);
  118.  fputs("% cxref program (c) Andrew M. Bishop 1995,96,97.\n",of);
  119.  fputs("\n",of);
  120.  
  121.  /*+ The file structure is broken into its components and they are each written out. +*/
  122.  
  123.  WriteLatexFilePart(file);
  124.  
  125.  if(file->includes)
  126.    {
  127.     Include inc =file->includes;
  128.     fprintf(of,"\n\\subsection*{Included Files}\n\n");
  129.     do{
  130.        if(inc!=file->includes)
  131.           fprintf(of,"\\medskip\n");
  132.        WriteLatexInclude(inc);
  133.       }
  134.     while((inc=inc->next));
  135.    }
  136.  
  137.  if(file->defines)
  138.    {
  139.     Define def =file->defines;
  140.     fprintf(of,"\n\\subsection*{Preprocessor definitions}\n\n");
  141.     do{
  142.        if(def!=file->defines)
  143.           fprintf(of,"\\medskip\n");
  144.        WriteLatexDefine(def);
  145.       }
  146.     while((def=def->next));
  147.    }
  148.  
  149.  if(file->typedefs)
  150.    {
  151.     Typedef type=file->typedefs;
  152.     fprintf(of,"\n\\subsection{Type definitions}\n\n");
  153.     do{
  154.        WriteLatexTypedef(type,file->name);
  155.       }
  156.     while((type=type->next));
  157.    }
  158.  
  159.  if(file->variables)
  160.    {
  161.     int any_to_mention=0;
  162.     Variable var=file->variables;
  163.  
  164.     do{
  165.        if(var->scope&(GLOBAL|LOCAL|EXTERNAL|EXTERN_F))
  166.           any_to_mention=1;
  167.       }
  168.     while((var=var->next));
  169.  
  170.     if(any_to_mention)
  171.       {
  172.        int first_ext=1,first_local=1;
  173.        Variable var=file->variables;
  174.        fprintf(of,"\n\\subsection{Variables}\n\n");
  175.        do{
  176.           if(var->scope&GLOBAL)
  177.              WriteLatexVariable(var,file->name);
  178.          }
  179.        while((var=var->next));
  180.        var=file->variables;
  181.        do{
  182.           if(var->scope&(EXTERNAL|EXTERN_F) && !(var->scope&GLOBAL))
  183.             {
  184.              if(first_ext)
  185.                {fprintf(of,"\n\\subsubsection{External Variables}\n\n"); first_ext=0;}
  186.              else
  187.                 fprintf(of,"\\medskip\n");
  188.              WriteLatexVariable(var,file->name);
  189.             }
  190.          }
  191.        while((var=var->next));
  192.        var=file->variables;
  193.        do{
  194.           if(var->scope&LOCAL)
  195.             {
  196.              if(first_local)
  197.                {fprintf(of,"\n\\subsubsection{Local Variables}\n\n"); first_local=0;}
  198.              else
  199.                 fprintf(of,"\\medskip\n");
  200.              WriteLatexVariable(var,file->name);
  201.             }
  202.          }
  203.        while((var=var->next));
  204.       }
  205.    }
  206.  
  207.  if(file->functions)
  208.    {
  209.     Function func=file->functions;
  210.     fprintf(of,"\n\\subsection{Functions}\n\n");
  211.     do{
  212.        if(func->scope&(GLOBAL|EXTERNAL))
  213.           WriteLatexFunction(func,file->name);
  214.       }
  215.     while((func=func->next));
  216.     func=file->functions;
  217.     do{
  218.        if(func->scope&LOCAL)
  219.           WriteLatexFunction(func,file->name);
  220.       }
  221.     while((func=func->next));
  222.    }
  223.  
  224.  fclose(of);
  225.  
  226. /* Clear the memory in latex() */
  227.  
  228.  latex(NULL); latex(NULL); latex(NULL); latex(NULL);
  229. }
  230.  
  231.  
  232. /*++++++++++++++++++++++++++++++++++++++
  233.   Write a File structure out.
  234.  
  235.   File file The File to output.
  236.   ++++++++++++++++++++++++++++++++++++++*/
  237.  
  238. static void WriteLatexFilePart(File file)
  239. {
  240.  int i;
  241.  
  242.  fprintf(of,"\\markboth{File %s}{File %s}\n",latex(file->name),latex(file->name));
  243.  fprintf(of,"\\section{File %s}\n",latex(file->name));
  244.  fprintf(of,"\\label{file_%s}\n\n",file->name);
  245.  
  246.  if(file->comment)
  247.     if(option_verbatim_comments)
  248.        fprintf(of,"\\begin{verbatim}\n%s\n\\end{verbatim}\n\n",latex(file->comment));
  249.     else
  250.       {
  251.        char *rcs1=strstr(file->comment,"$Header"),*rcs2=NULL;
  252.        if(rcs1)
  253.          {
  254.           rcs2=strstr(&rcs1[1],"$");
  255.           if(rcs2)
  256.             {
  257.              rcs2[0]=0;
  258.              fprintf(of,"{\\bf RCS %s}\n\n",latex(&rcs1[1]));
  259.              fprintf(of,"\\smallskip\n");
  260.              rcs2[0]='$';
  261.             }
  262.          }
  263.        if(rcs2)
  264.           fprintf(of,"%s\n\n",latex(&rcs2[2]));
  265.        else
  266.           fprintf(of,"%s\n\n",latex(file->comment));
  267.       }
  268.  
  269.  if(file->inc_in->n)
  270.    {
  271.     int i;
  272.  
  273.     if(file->comment)
  274.        fprintf(of,"\\medskip\n");
  275.     fprintf(of,"\\begin{cxreftabii}\nIncluded in:");
  276.     for(i=0;i<file->inc_in->n;i++)
  277.       {/* Allow a break in every 8 (or so) items to stop allow the table to break over the page. */
  278.        if(min(i,file->inc_in->n-i)%8 == 4)
  279.           fprintf(of,"\\cxreftabbreak{cxreftabii}\n");
  280.        fprintf(of,"\\ & %s & \\cxreffile{%s}\\\\\n",latex(file->inc_in->s[i]),file->inc_in->s[i]);
  281.       }
  282.     fprintf(of,"\\end{cxreftabii}\n\n");
  283.    }
  284.  
  285.  if(file->f_refs->n || file->v_refs->n)
  286.    {
  287.     int tabcount=0;
  288.     fprintf(of,"\\smallskip\n");
  289.     fprintf(of,"\\begin{cxreftabiii}\n");
  290.  
  291.     if(file->f_refs->n)
  292.       {
  293.        int others=0;
  294.        for(i=0;i<file->f_refs->n;i++)
  295.          {
  296.           if(file->f_refs->s2[i])
  297.             {
  298.              if(++tabcount%8 == 4)
  299.                 fprintf(of,"\\cxreftabbreak{cxreftabiii}\n");
  300.              if(i==others)
  301.                 fprintf(of,"Refs Func:");
  302.              fprintf(of,"\\ & %s() & %s & \\cxreffunc{%s}{%s}\\\\\n",latex(file->f_refs->s1[i]),latex(file->f_refs->s2[i]),file->f_refs->s1[i],file->f_refs->s2[i]);
  303.             }
  304.           else
  305.              others++;
  306.          }
  307.  
  308.        if(others)
  309.          {
  310.           if(others==f